{% extends "base.html" %}

{% set current_menu = 'work_hour' %}
{% set current_page = 'work_hour_management' %}

{% block title %}工时管理{% endblock %}

{% block content %}
<div class="container mx-auto">
    <!-- 面包屑导航 -->
    <nav class="text-sm breadcrumbs mb-6">
        <ol class="flex items-center space-x-2">
            <li><a href="{{ url_for('main.home') }}" class="text-gray-500 hover:text-gray-700">首页</a></li>
            <li><i class="fas fa-chevron-right text-gray-400 text-xs mx-2"></i></li>
            <li><a href="{{ url_for('work_hour.stats') }}" class="text-gray-500 hover:text-gray-700">工时统计</a></li>
            <li><i class="fas fa-chevron-right text-gray-400 text-xs mx-2"></i></li>
            <li class="text-gray-700">工时管理</li>
        </ol>
    </nav>

    <!-- 工时管理概览 -->
    <div class="grid grid-cols-1 md:grid-cols-4 gap-4 mb-6">
        <div class="bg-white rounded-lg shadow p-4">
            <h3 class="text-lg font-semibold text-gray-700">总工时</h3>
            <p class="text-3xl font-bold text-blue-600">{{ total_work_hours|round(1) }}</p>
            <p class="text-sm text-gray-500">小时</p>
        </div>
        <div class="bg-white rounded-lg shadow p-4">
            <h3 class="text-lg font-semibold text-gray-700">本月工时</h3>
            <p class="text-3xl font-bold text-green-600">{{ monthly_work_hours|round(1) }}</p>
            <p class="text-sm text-gray-500">小时</p>
        </div>
        <div class="bg-white rounded-lg shadow p-4">
            <h3 class="text-lg font-semibold text-gray-700">进行中任务</h3>
            <p class="text-3xl font-bold text-indigo-600">{{ active_tasks_count }}</p>
        </div>
        <div class="bg-white rounded-lg shadow p-4">
            <h3 class="text-lg font-semibold text-gray-700">工时偏差</h3>
            <p class="text-3xl font-bold text-yellow-600">{{ work_hour_deviation|round(1) }}</p>
            <p class="text-sm text-gray-500">小时</p>
        </div>
    </div>

    

    <!-- 最近工时记录 -->
    <div class="bg-white rounded-lg shadow-md">
        <div class="px-6 py-4 border-b border-gray-200 flex justify-between items-center">
            <h3 class="text-lg font-medium text-gray-900">最近工时记录</h3>
            <div class="relative">
                <button id="dropdownMenuButton" class="inline-flex items-center text-gray-500 hover:text-gray-700 focus:outline-none" onclick="toggleDropdown()">
                    <i class="fas fa-ellipsis-v"></i>
                </button>
                <div id="dropdownMenu" class="hidden absolute right-0 mt-2 w-48 bg-white rounded-md shadow-lg z-10">
                    <a href="{{ url_for('work_hour.stats') }}" class="block px-4 py-2 text-sm text-gray-700 hover:bg-gray-100">查看全部</a>
                    <a href="{{ url_for('work_hour.create') }}" class="block px-4 py-2 text-sm text-gray-700 hover:bg-gray-100">添加新记录</a>
                </div>
            </div>
        </div>
        <div class="p-6">
            {% if recent_work_hours %}
            <div class="overflow-x-auto">
                <table class="min-w-full divide-y divide-gray-200" id="recentWorkHoursTable">
                    <thead class="bg-gray-50">
                        <tr>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">项目</th>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">任务</th>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">工时</th>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">开始时间</th>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">结束时间</th>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">记录人</th>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">操作</th>
                        </tr>
                    </thead>
                    <tbody class="bg-white divide-y divide-gray-200">
                        {% for record in recent_work_hours %}
                        <tr class="hover:bg-gray-50">
                            <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">{{ record.project_name }}</td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">{{ record.task_name }}</td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">{{ record.hours|round(1) }} 小时</td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
                                  {% if record.start_time %}
                                      {{ record.start_time }}
                                  {% else %}
                                      未记录
                                  {% endif %}
                              </td>
                              <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
                                  {% if record.end_time %}
                                      {{ record.end_time }}
                                  {% else %}
                                      未记录
                                  {% endif %}
                              </td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">{{ record.user_name }}</td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm font-medium">
                                <a href="{{ url_for('work_hour.stats', project_id=record.project_id) }}" class="text-indigo-600 hover:text-indigo-900">
                                    <i class="fas fa-eye mr-1"></i> 查看
                                </a>
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
            {% else %}
            <div class="text-center py-8">
                <p class="text-gray-500 mb-4">暂无工时记录</p>
                <a href="{{ url_for('work_hour.create') }}" class="inline-flex items-center px-4 py-2 border border-transparent text-sm font-medium rounded-md text-white bg-blue-600 hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500">
                    <i class="fas fa-plus mr-2"></i> 添加工时记录
                </a>
            </div>
            {% endif %}
        </div>
    </div>
</div>

<!-- 页面特定的JavaScript -->
{% block scripts %}
<script>
    // 下拉菜单切换功能
    function toggleDropdown() {
        const dropdown = document.getElementById('dropdownMenu');
        dropdown.classList.toggle('hidden');
    }

    // 点击页面其他地方关闭下拉菜单
    document.addEventListener('click', function(event) {
        const dropdownButton = document.getElementById('dropdownMenuButton');
        const dropdown = document.getElementById('dropdownMenu');
        
        if (!dropdownButton.contains(event.target) && !dropdown.contains(event.target)) {
            dropdown.classList.add('hidden');
        }
    });

    // 表格排序功能
    document.addEventListener('DOMContentLoaded', function() {
        const table = document.getElementById('recentWorkHoursTable');
        if (table) {
            const tbody = table.querySelector('tbody');
            const rows = Array.from(tbody.querySelectorAll('tr'));
            
            // 按记录日期降序排列
            rows.sort((a, b) => {
                const dateA = new Date(a.cells[3].textContent.trim());
                const dateB = new Date(b.cells[3].textContent.trim());
                return dateB - dateA;
            });
            
            // 重新排列行
            rows.forEach(row => tbody.appendChild(row));
            
            // 添加表头点击排序功能
            const headers = table.querySelectorAll('th');
            headers.forEach((header, index) => {
                header.style.cursor = 'pointer';
                header.addEventListener('click', () => {
                    sortTable(table, index);
                });
            });
        }
    });

    // 表格排序函数
    function sortTable(table, columnIndex) {
        const tbody = table.querySelector('tbody');
        const rows = Array.from(tbody.querySelectorAll('tr'));
        const isAscending = table.dataset.sortOrder !== 'asc';
        
        rows.sort((a, b) => {
            const aValue = a.cells[columnIndex].textContent.trim();
            const bValue = b.cells[columnIndex].textContent.trim();
            
            // 尝试解析日期
            if (columnIndex === 3) { // 工作日期列
                const dateA = new Date(aValue);
                const dateB = new Date(bValue);
                return isAscending ? dateA - dateB : dateB - dateA;
            }
            
            // 尝试解析数字
            const aNum = parseFloat(aValue);
            const bNum = parseFloat(bValue);
            if (!isNaN(aNum) && !isNaN(bNum)) {
                return isAscending ? aNum - bNum : bNum - aNum;
            }
            
            // 默认字符串比较
            return isAscending ? 
                aValue.localeCompare(bValue) : 
                bValue.localeCompare(aValue);
        });
        
        // 重新排列行
        rows.forEach(row => tbody.appendChild(row));
        
        // 更新排序状态
        table.dataset.sortOrder = isAscending ? 'asc' : 'desc';
        
        // 更新排序指示器
        const headers = table.querySelectorAll('th');
        headers.forEach((header, i) => {
            const indicator = header.querySelector('.sort-indicator');
            if (indicator) {
                indicator.remove();
            }
            
            if (i === columnIndex) {
                const indicatorElement = document.createElement('span');
                indicatorElement.className = 'sort-indicator ml-1';
                indicatorElement.textContent = isAscending ? ' ↑' : ' ↓';
                header.appendChild(indicatorElement);
            }
        });
    }
</script>
{% endblock %}
{% endblock %}